﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Model;
using System.Data;
using BLL;
using System.Configuration;

public partial class PersonInfo_GiftEdit : BasePage
{
    //private Access access = new Access();
    PRODUCTBO gbo = new PRODUCTBO();
    private static int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            if (GetQeuryString("para") == "update")
            {
                string id = GetQeuryString("id");
                PRODUCT gift = gbo.GetModel(int.Parse(id));
                InitData(gift);
            }
            else if (GetQeuryString("para") == "add")
            {
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        PRODUCT gift = GetEntity();
        if (GetQeuryString("para") == "add")
        {
            
                gbo.Add(gift);
                AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
            
        }
        else if (GetQeuryString("para") == "update")
        {
            gbo.Update(gift);
            AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
        }
    }

    #region 私有方法
    private PRODUCT GetEntity()
    {
        PRODUCT gift = new PRODUCT();
        gift.States=1;
        if (GetQeuryString("para") == "update")
        {
            gift = gbo.GetModel(Convert.ToInt32(GetQeuryString("id")));
        }
        gift.CreateDate = DateTime.Now;
        gift.Explain = txtExplain.Text;
        gift.Integral = int.Parse(txtIntegral.Text);
        gift.Name = txtName.Text;
        gift.PicUrl = txtPicUrl.Text;
        gift.PicUrl2 = txtPicUrl2.Text;
        gift.PicUrl3 = txtPicUrl3.Text;
        gift.PicUrl4 = txtPicUrl4.Text;
        gift.PicUrl5 = txtPicUrl5.Text;
        gift.PicUrl6 = txtPicUrl6.Text;
        gift.IF_NEW = CheckBox4.Checked ? 1 : 0;
        gift.IF_SALES = CheckBox2.Checked ? 1 : 0;
        gift.IF_STAR = CheckBox1.Checked ? 1 : 0;
        gift.IF_TOP = CheckBox3.Checked ? 1 : 0;
        gift.IF_DISP = CheckBox5.Checked ? 1 : 0;
        gift.IF_REDP = CheckBox6.Checked ? 1 : 0;
        gift.T_LEVEL1 = txtDL.Text;
        gift.T_LEVEL2 = txtXL.Text;
        gift.Details = content1.InnerText;// txtDetails.Text;
        return gift;
    }
    private void InitData(PRODUCT gift)
    {
        this.txtName.Text = gift.Name;
        txtExplain.Text = gift.Explain;
        txtIntegral.Text = gift.Integral.ToString();
        txtPicUrl.Text = gift.PicUrl;;
        Image1.ImageUrl = gift.PicUrl;
        txtPicUrl2.Text = gift.PicUrl2;
        Image2.ImageUrl = gift.PicUrl2;
        txtPicUrl3.Text = gift.PicUrl3;
        Image3.ImageUrl = gift.PicUrl3;
        txtPicUrl4.Text = gift.PicUrl4;
        Image4.ImageUrl = gift.PicUrl4;
        txtPicUrl5.Text = gift.PicUrl5;
        Image5.ImageUrl = gift.PicUrl5;
        txtPicUrl6.Text = gift.PicUrl6;
        Image6.ImageUrl = gift.PicUrl6;
        txtBegin_Inv.Text = gift.begin_inv.ToString();
        txtPrice.Text = gift.Price.ToString();
        txtREDEP_INV.Text = gift.REDP_INV.ToString();
        txtSALES_INV.Text = gift.SALES_INV.ToString();
       // txtDetails.Text = gift.Details;
        content1.InnerText = gift.Details;
        txtEXCH_NUM.Text = gift.EXCH_NUM.ToString();
        if (txtPicUrl.Text.Trim() != "")
        {
            tr1.Visible = true;
            tr2.Visible = true;
        }
        if (txtPicUrl2.Text.Trim() != "")
        {
            tr3.Visible = true;
            tr4.Visible = true;
            Image2.Visible = true;
        }
        if (txtPicUrl3.Text.Trim() != "")
        {
            tr5.Visible = true;
            tr6.Visible = true;
            Image3.Visible = true;
        }
        if (txtPicUrl4.Text.Trim() != "")
        {
            tr7.Visible = true;
            tr8.Visible = true;
            Image4.Visible = true;
        }
        if (txtPicUrl5.Text.Trim() != "")
        {
            tr9.Visible = true;
            tr9.Visible = true;
            Image5.Visible = true;
        }
        if (txtPicUrl6.Text.Trim() != "")
        {
            tr3.Visible = true;
            tr4.Visible = true;
            Image6.Visible = true;
        }
        txtGiftNo.Text = gift.SKU;
        CheckBox1.Checked = gift.IF_STAR == 1 ? true : false;
        CheckBox2.Checked = gift.IF_SALES == 1 ? true : false;
        CheckBox3.Checked = gift.IF_TOP == 1 ? true : false;
        CheckBox4.Checked = gift.IF_NEW == 1 ? true : false;
        CheckBox5.Checked = gift.IF_DISP == 1 ? true : false;
        CheckBox6.Checked = gift.IF_REDP == 1 ? true : false;
        txtDL.Text = gift.T_LEVEL1;
        txtXL.Text = gift.T_LEVEL2;
        id = gift.ID;
    }

    #endregion
    protected void Button2_Click(object sender, EventArgs e)
    {
        UploadFile();
    }

    private void UploadFile()
    {
        string FileName = file.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = file.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                else
                {
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        file.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image1.ImageUrl = "Images/" + guid + ".jpg";
                            txtPicUrl.Text = ConfigurationManager.AppSettings["ServerURL"] + "/PersonInfo/Images/" + guid + ".jpg";
                            tr1.Visible = true;
                            tr2.Visible = true;
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
    }
    /// <summary>
    /// 图片2上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button3_Click(object sender, EventArgs e)
    {
        string FileName = FileUpload1.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = FileUpload1.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                else
                {
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        FileUpload1.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image2.ImageUrl = "Images/" + guid + ".jpg";
                            Image2.Visible = true;
                            tr3.Visible = true;
                            tr4.Visible = true;
                            txtPicUrl2.Text = ConfigurationManager.AppSettings["ServerURL"] + "/PersonInfo/Images/" + guid + ".jpg";
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
    }

    /// <summary>
    /// 图片3上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button4_Click(object sender, EventArgs e)
    {
        string FileName = FileUpload2.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = FileUpload2.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                else
                {
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        FileUpload2.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image3.ImageUrl = "Images/" + guid + ".jpg";
                            Image3.Visible = true;
                            tr5.Visible = true;
                            tr6.Visible = true;
                            txtPicUrl3.Text = ConfigurationManager.AppSettings["ServerURL"] + "/PersonInfo/Images/" + guid + ".jpg";
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
    }

    /// <summary>
    /// 图片4上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button5_Click(object sender, EventArgs e)
    {
        string FileName = FileUpload3.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = FileUpload3.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                else
                {
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        FileUpload3.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image4.ImageUrl = "Images/" + guid + ".jpg";
                            Image4.Visible = true;
                            tr7.Visible = true;
                            tr8.Visible = true;
                            txtPicUrl4.Text = ConfigurationManager.AppSettings["ServerURL"] + "/PersonInfo/Images/" + guid + ".jpg";
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
    }

    /// <summary>
    /// 图片5上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button6_Click(object sender, EventArgs e)
    {
        string FileName = FileUpload4.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = FileUpload4.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                else
                {
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        FileUpload4.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image5.ImageUrl = "Images/" + guid + ".jpg";
                            Image5.Visible = true;
                            tr9.Visible = true;
                            tr10.Visible = true;
                            txtPicUrl5.Text = ConfigurationManager.AppSettings["ServerURL"] + "/PersonInfo/Images/" + guid + ".jpg";
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
    }
    /// <summary>
    /// 图片6上传
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button7_Click(object sender, EventArgs e)
    {
        string FileName = FileUpload5.FileName;
        string path = HttpContext.Current.Request.MapPath("Images/");

        if (!string.IsNullOrEmpty(FileName))
        {
            string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
            if ((extension.ToLower() == ".jpg"))
            {
                int fileSize = FileUpload5.PostedFile.ContentLength;
                if (fileSize == 0)
                {
                    base.AlertMessage("文件不存在！");
                }
                else
                {
                    if (true)//限制大小为1M
                    {
                        string guid = Guid.NewGuid().ToString();
                        FileUpload5.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
                        if (File.Exists(path + guid + ".jpg"))
                        {
                            Image6.ImageUrl = "Images/" + guid + ".jpg";
                            Image6.Visible = true;
                            txtPicUrl6.Text = ConfigurationManager.AppSettings["ServerURL"] + "/PersonInfo/Images/" + guid + ".jpg";
                        }
                    }
                }
            }
            else
            {
                AlertMessage("请上传JPG格式图片!");
            }
        }
        else
        {
            AlertMessage("请选择文件.");
        }
    }
}